;;; Set the debug option to enable a backtrace when a problem occurs.
(setq debug-on-error t)

;; ELPA
(require 'package)
(package-initialize)
(add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t)

;;auto-complete
(require 'auto-complete)
(require 'auto-complete-config)
(ac-config-default)

(require 'auto-complete-nxml)

(defun my:ac-c-header-init ()
  (require 'auto-complete-c-headers)
  (add-to-list 'ac-sources 'ac-source-c-headers)
  (add-to-list 'achead:include-directories '"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include")
)
(add-hook 'c++-mode-hook 'my:ac-c-header-init)
(add-hook 'c-mode-hook 'my:ac-c-header-init)


;;yasnippet
(require 'yasnippet)
(yas-global-mode 1)


;; fix iedit bug on Mac
(define-key global-map (kbd "C-c ;") 'iedit-mode)


;; flymake-google-cpplint
(defun my:flymake-google-init ()
  (require 'flymake-google-cpplint)
  (custom-set-variables '(flymake-google-cpplint-command "/Users/xuhj/bin/cpplint")
			'(flymake-google-cpplint-verbose "3")
			'(flymake-google-cpplint-linelength "120"))
  (flymake-google-cpplint-load)
)
;;(add-hook 'c++-mode-hook 'my:flymake-google-init)
;;(add-hook 'c-mode-hook 'my:flymake-google-init)


;; google-c-style
(require 'google-c-style)
(add-hook 'c-mode-common-hook 'google-set-c-style)
(add-hook 'c-mode-common-hook 'google-make-newline-indent)

;;(setq-default c-basic-offset 4
;;      tab-width 4
;;      indent-tabs-mode t)

;; elpy
;;(elpy-enable)
;; Fixing a key binding bug in elpy 
;;(define-key yas-minor-mode-map (kbd "C-c k") 'yas-expand)
;; Fixing another key binding bug in iedit mode
;;(define-key global-map (kbd "C-c o") 'iedit-mode)

;; pymacs
(add-to-list 'load-path "~/.emacs.d/add-on/pymacs")
(autoload 'pymacs-apply "pymacs")
(autoload 'pymacs-call "pymacs")
(autoload 'pymacs-eval "pymacs" nil t)
(autoload 'pymacs-exec "pymacs" nil t)
(autoload 'pymacs-load "pymacs" nil t)
(autoload 'pymacs-autoload "pymacs")


;; python-mode.el
(when (featurep 'python) (unload-feature 'python t))
(setq py-shell-name "ipython")
(add-to-list 'load-path "~/.emacs.d/add-on/python-mode/")
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(require 'python-mode)


;;git mergetool 使用ediff ,前提可以正常使用emacsclient ,并且Emacs已经启动。
;; ~/.gitconfig
;; [mergetool "ediff"]
;; cmd = emacsclient --eval \"(git-mergetool-emacsclient-ediff \\\"$LOCAL\\\" \\\"$REMOTE\\\" \\\"$BASE\\\" \\\"$MERGED\\\")\"
;; trustExitCode = false
;; [mergetool]
;; prompt = false
;; [merge]
;; tool = ediff
;;
;; Setup for ediff.
;;
;;(require 'ediff)

(defvar ediff-after-quit-hooks nil
  "* Hooks to run after ediff or emerge is quit.")

(defadvice ediff-quit (after edit-after-quit-hooks activate)
  (run-hooks 'ediff-after-quit-hooks))

(setq git-mergetool-emacsclient-ediff-active nil)

(defun local-ediff-frame-maximize ()
  (when (boundp 'display-usable-bounds)
    (let* ((bounds (display-usable-bounds))
           (x (nth 0 bounds))
           (y (nth 1 bounds))
           (width (/ (nth 2 bounds) (frame-char-width)))
           (height (/ (nth 3 bounds) (frame-char-height))))
      (set-frame-width (selected-frame) width)
      (set-frame-height (selected-frame) height)
      (set-frame-position (selected-frame) x y))  )
  )
(setq ediff-window-setup-function 'ediff-setup-windows-plain)
(setq ediff-split-window-function 'split-window-horizontally)

(defun local-ediff-before-setup-hook ()
  (setq local-ediff-saved-frame-configuration (current-frame-configuration))
  (setq local-ediff-saved-window-configuration (current-window-configuration))
  (local-ediff-frame-maximize)
  (if git-mergetool-emacsclient-ediff-active
      (raise-frame)))

(defun local-ediff-quit-hook ()
  (set-frame-configuration local-ediff-saved-frame-configuration)
  (set-window-configuration local-ediff-saved-window-configuration))

(defun local-ediff-suspend-hook ()
  (set-frame-configuration local-ediff-saved-frame-configuration)
  (set-window-configuration local-ediff-saved-window-configuration))

(add-hook 'ediff-before-setup-hook 'local-ediff-before-setup-hook)
(add-hook 'ediff-quit-hook 'local-ediff-quit-hook 'append)
(add-hook 'ediff-suspend-hook 'local-ediff-suspend-hook 'append)

;; Useful for ediff merge from emacsclient.
(defun git-mergetool-emacsclient-ediff (local remote base merged)
  (setq git-mergetool-emacsclient-ediff-active t)
  (if (file-readable-p base)
      (ediff-merge-files-with-ancestor local remote base nil merged)
    (ediff-merge-files local remote nil merged))
  (recursive-edit))

(defun git-mergetool-emacsclient-ediff-after-quit-hook ()
  (exit-recursive-edit))

(add-hook 'ediff-after-quit-hooks 'git-mergetool-emacsclient-ediff-after-quit-hook 'append)


;; MIT-Scheme
;;(require 'xscheme)


;; hide-lines
(add-to-list 'load-path "~/.emacs.d/add-on/hide-lines/")
(autoload 'hide-lines "hide-lines" "Hide lines based on a regexp" t)
(global-set-key "\C-ch" 'hide-lines)

;; CEDET
(load-file "~/.emacs.d/add-on/cedet/common/cedet.elc")

;; Enable EDE (Project Management) features
(global-ede-mode t)

;; Enable all semantic features that provide coding assistance.
(semantic-load-enable-guady-code-helpers)
;; Enable all semantic features that assist with debugging semantic
(semantic-load-enable-semantic-debugging-helpers)

;; add semantic as a suggestion backend to auto complete
(defun my:add-semntic-to-autocomplete ()
  (add-to-list 'ac-sources 'ac-source-semantic)
)
(add-hook 'c-mode-common-hook 'my:add-semntic-to-autocomplete)

;; Enable semanticdb
(require 'semanticdb)
(global-semanticdb-minor-mode 1)

;;(require 'semanticdb-global)
(setq cedet-global-command "/opt/local/bin/global")
(when (cedet-gnu-global-version-check t)
      (semanticdb-enable-gnu-global-databases 'c-mode t)
      (semanticdb-enable-gnu-global-databases 'c++-mode t)
      (semanticdb-enable-gnu-global-databases 'jde-mode t))
(setq ede-locate-setup-options
      '(ede-locate-global
	ede-locate-base))


;; Enable SRecode
(srecode-minor-mode 1)

;; Enable generic project loaders
(ede-enable-generic-projects)


;; ECB
(add-to-list 'load-path "~/.emacs.d/add-on/ecb/")
(require 'ecb)


;; JDEE
(add-to-list 'load-path "~/.emacs.d/add-on/jdee/lisp/")
(setq jde-help-remote-file-exists-function '("beanshell"))
(add-to-list 'auto-mode-alist '("\\.java\\'" . jde-mode))
(load "jde")


;; gas-mode
(add-to-list 'load-path (expand-file-name "~/.emacs.d/add-on/gas-mode/"))
(require 'gas-mode)
(add-to-list 'auto-mode-alist '("\\.S\\'" . gas-mode))
